package com.gem.day10;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 本类用来演示异常处理机制
 * 1.抛出异常
 * 2.捕获异常
 * 
 * @author easonsy
 * 2020年7月30日 上午11:05:03
 */
public class ExceptionDemo2 {

	public static void main(String[] args) {
		/*Scanner scanner = new Scanner(System.in);
		System.out.println("请您输入数字");
		try {
			int number = scanner.nextInt();
			System.out.println(number);
		} catch (InputMismatchException e) {
			//e.printStackTrace();
			System.out.println("输出有误!");
		}
		scanner.close();*/

		try {
			System.out.println(divide(3, 0));
		} catch (ArithmeticException e) {
			System.err.println("除数不能为0!");
		}
	}

	public static double divide(int a, int b) {
		if (b == 0) {
			throw new ArithmeticException("除数不能为0!");
		}
		return a / b;
	}

	public static void method() {
		try {
			method1();
			method2();
			method3();
		} catch (ParseException e) {
			//e.printStackTrace();
			//处理ParseException
		} catch (InterruptedException e) {
			e.printStackTrace();
			//处理InterruptedException
		}

		//JDK7.0提供
		try {
			method1();
			method2();
			method3();
		} catch (ParseException | InterruptedException e) {
			//统一处理
		}
	}

	public static void method1() throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse("aaaa");
		System.out.println(date);
	}

	public static void method2() throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse("bbbb");
		System.out.println(date);
	}

	public static void method3() throws InterruptedException, ParseException {
		Thread.sleep(1000);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse("cccc");
		System.out.println(date);
	}
}
